package com.ukao.pad.dialog;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.ukao.pad.R;
import com.ukao.pad.adapter.StoreNameTagAdapter;
import com.ukao.pad.base.BaseDialogFragment;
import com.ukao.pad.bean.StoreInfoBean;
import com.ukao.pad.listener.OnDialogSureClickListener;
import com.ukao.pad.widget.StateImageView;
import com.ukao.pad.widget.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 *门店选择对话框
 */
public class StoreChooseDialogFragment extends BaseDialogFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private  Unbinder unbinder;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    @BindView(R.id.tag_flow_layout)
    TagFlowLayout mListView;
    @BindView(R.id.img_close_btn)
    StateImageView closeWindow;
    @BindView(R.id.btn_submit)
    Button submitBtn;
    @BindView(R.id.sb_cancel)
    Button cancelBtn;
    private  ArrayList<StoreInfoBean> storeListData;  //门店数据源
    private int selectPos;
    private StoreNameTagAdapter mTagAdapter;
    public StoreChooseDialogFragment() {

    }
    public static StoreChooseDialogFragment newInstance(ArrayList<StoreInfoBean> list) {
        StoreChooseDialogFragment fragment = new StoreChooseDialogFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList(ARG_PARAM1,list);
        fragment.setArguments(args);
        return fragment;
    }
    private OnDialogSureClickListener mDialogConfirmClickListener;
    public  void  setOnDialogSureClickListener(OnDialogSureClickListener mOnItemChildClickListener){
        this.mDialogConfirmClickListener=mOnItemChildClickListener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            storeListData = getArguments().getParcelableArrayList(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.popwindow_select_stores, container, false);
        unbinder=ButterKnife.bind(this,view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);  //点击外面不取消
        initViews();  //初始化Views
    }
   private void  initViews(){
       mTagAdapter =new StoreNameTagAdapter(getActivity(),storeListData);
       mListView.setAdapter(mTagAdapter);
       mTagAdapter.setSelectedList(0);
       mListView.setOnSelectListener(mOnSelectListener);
   }
    private TagFlowLayout.OnSelectListener mOnSelectListener = new TagFlowLayout.OnSelectListener() {
        @Override
        public void onSelected(Set<Integer> selectPosSet) {
            for (Integer pos :
                    selectPosSet) {
                selectPos = pos;
            }
//            if (mItemSelectListener != null) {
//                Object mitem = mAdapter.getItem(selectPos);
//                mItemSelectListener.onSureItemClick(mitem);
//            }
        }
    };
    @OnClick({R.id.img_close_btn,R.id.btn_submit,R.id.sb_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.img_close_btn:
                dismiss();
                break;
            case  R.id.btn_submit:
                mDialogConfirmClickListener.onSureItemClick(mTagAdapter.getItem(selectPos));
                break;
            case R.id.sb_cancel:
                dismiss();
                break;
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mTagAdapter=null;
        unbinder.unbind(); //解绑
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


}
